package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ProductDaoImpl;
import com.ysd.entity.Product;
import com.ysd.util.BaseServlet;

public class AdminProductServlet extends BaseServlet {
	
	/**
	 * 多条件查询商品
	 * localhost:8080/shangcheng/adminProduct?reqName=getProducts
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProducts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接受前台传来的参数
		String productId = request.getParameter("productId");
		String productName = request.getParameter("productName"); //鸡蛋
		String productMaterial = request.getParameter("productMaterial");
		
		int beginProductPrice = 0;
		if(request.getParameter("beginProductPrice")!=null && !request.getParameter("beginProductPrice").equals("")) {
			beginProductPrice = Integer.parseInt(request.getParameter("beginProductPrice")); //NumberFormatException 数字格式化异常
		}
		
		int endProductPrice = 0;
		if(request.getParameter("endProductPrice")!=null && !request.getParameter("endProductPrice").equals("")) {
			endProductPrice = Integer.parseInt(request.getParameter("endProductPrice")); //NumberFormatException 数字格式化异常
		}
		
		String brand = request.getParameter("brand");
		String isPackage = request.getParameter("isPackage");
		String isOrder = request.getParameter("isOrder");
		String color = request.getParameter("color");
		String element = request.getParameter("element");
		String style = request.getParameter("style");
		String stylePositioning = request.getParameter("stylePositioning");
		String imgs = request.getParameter("imgs");
		
		int categoryId = 0;
		if(request.getParameter("categoryId")!=null && !request.getParameter("categoryId").equals("")) {
			categoryId = Integer.parseInt(request.getParameter("categoryId"));//1
		}
		
		Product p = new Product(productId, productName, productMaterial, 0, beginProductPrice, endProductPrice, 0, brand, isPackage, isOrder, color, element, style, stylePositioning, imgs, categoryId, "");
		
		ProductDaoImpl pDao = new ProductDaoImpl();
		List<Product> pList = pDao.getProductsByWhere(p); //调用实现类中的方法, 得到查询出的产品集合
		
		Gson gson = new Gson();
		String res = gson.toJson(pList);//转化为json字符串
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		out.print(res);
		
		out.flush();
		out.close();
	}

}
